package com.study.talk.module.controller;

import com.aliyun.credentials.provider.StaticCredentialsProvider;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;
import com.study.talk.common.code.MessageCode;
import com.study.talk.common.resullt.FwResult;
import com.study.talk.common.util.SecurityUtil;
import com.study.talk.module.entity.Message;
import com.study.talk.module.entity.vo.PhoneSmsVo;
import com.study.talk.module.netty.message.TalkMessage;
import com.study.talk.module.service.MessageService;
import com.study.talk.module.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author thor
 * @since 2023-12-18
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private MessageService messageService;

    @Resource
    private UserService userService;


    private final Client client = new Client(new Config().setAccessKeyId("LTAI5tQPkVPi3ELZZ4F19b7A").setAccessKeySecret("TXEawwQdSezv0WTUtSA6X80GPNx1sp").setEndpoint("dysmsapi.aliyuncs.com"));

    public MessageController() throws Exception {
    }

    @GetMapping("/list/{userId}")
    @ApiOperation("获取缓存中的消息")
    public FwResult<List<Message>> getCacheList(@PathVariable("userId") Integer userId) {
        List<Message> messageList = messageService.getMessageList(userId);
        if (messageList == null || messageList.size() == 0) return FwResult.ok(new ArrayList<>());
        messageService.deleteMessageList(userId);
        return FwResult.ok(messageList);
    }

    @PostMapping("/sms/send")
    public FwResult<SendSmsResponse> sendSms(@RequestBody PhoneSmsVo phoneSms) throws Exception {
        SendSmsRequest request = new SendSmsRequest().setPhoneNumbers(phoneSms.getPhoneNumber()).setSignName("畅聊app").setTemplateCode("SMS_301445151").setTemplateParam("{ \"code\": \"4587\" }");

        SendSmsResponse response = client.sendSms(request);
        return FwResult.ok(response);
    }

    @GetMapping("/list/all")
    public FwResult<List<Message>> getAllMessage() {
        Integer userId = SecurityUtil.getUser().getId();
        return FwResult.ok(userService.getAllMessage(userId));
    }

}
